package 并发编程04.ThreadLocal关键字;

import java.util.Random;

/**
 * 作者：idea
 * 日期：2018/7/18
 * 描述：
 */
public class TestThreadLocal {
    public static class MyRunnable implements Runnable {

        private ThreadLocal<Integer> threadLocal = new ThreadLocal<Integer>();

        @Override
        public void run() {
            threadLocal.set(new Random().nextInt(10));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread()+": "+threadLocal.get());
        }
    }

    public static void main(String[] args) {
        System.out.println("start");
        MyRunnable m1=new MyRunnable();
        Thread t1=new Thread(m1);
        Thread t2=new Thread(m1);
        t1.start();
        t2.start();
    }
}
